package com.example.chat.config;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 * 配置类
 * 用于从 ServerEndpointConfig 中 获取 httpSession 对象
 * @author Lay.He
 * @date 2023/11/23 21:14
 */
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request,
        HandshakeResponse response) {

        HttpSession httpSession = (HttpSession)request.getHttpSession();
        //将 httpsession 存入 ServerEndpointConfig对象，方便于websocket在OnOpen()建立链接后获取
        sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
    }
}
